package com.qdxwx.web.controller.user;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.user.UserAddressService;
import com.qdxwx.models.user.User;
import com.qdxwx.models.user.UserAddress;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 我的收货地址
 */
@Controller
@RequestMapping("/user")
public class UserAddressController extends BaseController {

    @Autowired
    private UserAddressService userAddressService;

    @RequestMapping(value = "/address.html", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap mmap) {
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(LoginUser.getUserId(request));
        List<UserAddress> addressList = userAddressService.getList(userAddress, null);
        mmap.put("addressList",addressList);
        mmap.put("user",LoginUser.getUser(request));
        return getViewPath("ucenter/uc-address");
    }

    @RequestMapping(value = "/address/edit.html", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        UserAddress userAddress = userAddressService.get(id);
        if(userAddress == null) {
            userAddress = new UserAddress();
        }
        mmap.put("userAddress", userAddress);
        return getViewPath("ucenter/uc-address-edit");
    }

    @RequestMapping(value = "/address/save.json", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult save(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        UserAddress userAddress;
        if(id > 0){
             userAddress = userAddressService.get(id);
            if(userAddress == null) return AjaxResult.error("找不到指定记录");
        }else {
            userAddress = new UserAddress();
            userAddress.setUserId(LoginUser.getUserId(request));
            long count = userAddressService.count(userAddress);
            if(count >= 5) return AjaxResult.error("最多添加5条收货地址");
        }
        String province = request.getParameter("province");
        if(TKString.isEmpty(province)) return AjaxResult.error("请输入省份与地区");
        String address = request.getParameter("address");
        if(TKString.isEmpty(address)) return AjaxResult.error("请输入详细地址");
        String userName = request.getParameter("userName");
        if(TKString.isEmpty(userName)) return AjaxResult.error("请输入收件人名称");
        String mobile = request.getParameter("mobile");
        if(!TKString.isMobile(mobile)) return AjaxResult.error("请输入正确的手机号码");
        userAddress.setProvince(province);
        userAddress.setAddress(address);
        userAddress.setUserName(userName);
        userAddress.setMobile(mobile);
        int row;
        if(id > 0){
            row = userAddressService.update(userAddress);
        }else {
            row = userAddressService.insert(userAddress);
        }
        return row > 0 ? AjaxResult.success():AjaxResult.error();
    }

    @PostMapping(value = "/address/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        User user = LoginUser.getUser(request);
        if (user == null) {
            return AjaxResult.error("登录超时");
        }
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id < 1){
            return AjaxResult.error("参数异常");
        }
        UserAddress userAddress = userAddressService.get(id);
        if(user.getUserId() != userAddress.getUserId()){
            return AjaxResult.error("非法操作");
        }
        int row = userAddressService.delete(id);
        return row > 0 ? AjaxResult.success():AjaxResult.error();
    }

}
